<?php
/**
 * Moduleadmin_Data_Editmodule
 * edit module template data
 * 
 * @author Adrian Stolarski
 */
class Moduleadmin_Data_Editmodule implements Moduleadmin_Data_Mainable, 
        Moduleadmin_Data_Preparable, Moduleadmin_Data_Tokenable, 
        Moduleadmin_Data_Templatable{
    
    /**
     *
     * @var type random_token
     * random token handler
     */
    public $random_token;
    /**
     *
     * @var type $database
     * database handler
     */
    public $database;
    /**
     *
     * @var type $database_query
     * database query handler
     */
    public $database_query;
    /**
     *
     * @var type $access_level
     * access level handler
     */
    public $access_level;
    /**
     * constructor()
     */
    public function __construct() {
        $this->database = new Core_Driver_MysqlImproved;
        $this->access_level = new Core_Controller_Accesslevel;
    }
    
    /**
     * create_token()
     * create new random token
     * 
     * @return type Core_Data_Tokenizer
     */
    public function create_token() {
        $this->random_token = new Core_Data_Tokenizer;
        return $this->random_token->create_token();
    }
    
    /**
     * get_template_name()
     * get current template
     * 
     * @return string $template
     */
    public function get_template_name() {
        $template = SERVER_ROOT."moduleadmin/views/editmodule.php";
        return $template;
    }
    
    /**
     * main()
     * get edit page template data
     * 
     * @param array $variables
     * @return type $module_data
     */
    public function main(array $variables = null) {
        //var_dump($variables);
        //die();
        $page = array();
        $page['template'] = $this->get_template_name();
        $page['csrf_token'] = $this->create_token();
        $this->database->connect();
        foreach($variables as $key => $value){
            $variables[$key] = $this->database->escape($value);
        }
        //var_dump($variables);
        //die();
        $this->prepare_query($variables);
        //echo $this->database_query;
        //die();
        //var_dump($variables);
        //die();
        $this->database->prepare($this->database_query);
        $this->database->query();
        $page['module_data'] = $this->database->fetch('array');
        $variables['access_level_id'] = $page['module_data']['module_access_level'];
        $page['module_data']['translate_access_level'] = $this->access_level->main($variables);
        //var_dump($page);
        //die();
        return $page;
    }
    
    /**
     * prepare_query()
     * prepare database query
     * 
     * @param array $variables
     */
    public function prepare_query(array $variables = null) {
       $this->database_query = "SELECT * FROM page_module WHERE module_name='".$variables['module_name']."';"; 
    }

}
